package com.limox.bishe.http;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

 
public class HttpUtil {

	/**
	 * http请求：GET
	 * 
	 * @param url
	 *            请求url
	 * @param httpClient
	 *            客户端
	 * @param setHeader
	 *            header
	 * @return
	 * @throws IOException
	 */
	public static String getUrl(String url, DefaultHttpClient httpClient,
			String setHeader) throws IOException {
		System.out.println("url:"+url);
		 
		HttpGet request = new HttpGet(url);
		request.setHeader("Referer", setHeader); 
		
		
		HttpResponse response = httpClient.execute(request); 
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			return EntityUtils.toString(response.getEntity());
		} else {
			return "";
		}
	}

	/**
	 * 登陆请求
	 * 
	 * @param __VIEWSTATE
	 * @param username
	 *            账号
	 * @param pass
	 *            密码
	 * @return 
	 */
	public static String PostUrl(String __VIEWSTATE, String username,
			String pass, String myUrl, DefaultHttpClient mHttpClient) {
		
		
		List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("__VIEWSTATE", __VIEWSTATE));
		pairs.add(new BasicNameValuePair("txtUserName", username));
		pairs.add(new BasicNameValuePair("TextBox2", pass));
		pairs.add(new BasicNameValuePair("RadioButtonList1", "%D1%A7%C9%FA"));
		pairs.add(new BasicNameValuePair("Button1", null));
		pairs.add(new BasicNameValuePair("lbLanguage", null));
		String info = "";
		try {
			info = postUrl("http://" + myUrl + "/default2.aspx",
					pairs, mHttpClient, "");
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

		return info;
		
	}
	
	
	
	/**
	 * post提交数据
	 * 
	 * @param url
	 *            提交地址
	 * @param pairs
	 *            参数
	 * @param httpClient
	 *            httpClient          
	 * @param setHeader
	 *            Header
	 * @return 
	 * 			String
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String postUrl(String url, List<BasicNameValuePair> pairs,
			DefaultHttpClient httpClient, String setHeader)
			throws ClientProtocolException, IOException {

		HttpPost request = new HttpPost(url);
		request.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
		request.setHeader("Referer", setHeader);
		httpClient.getParams().setParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); // 设置请求超时时间
		httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
				10000); // 读取超时

		HttpResponse response = httpClient.execute(request);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			return EntityUtils.toString(response.getEntity());
		} else {
			return null;
		}

	}


}
